package cn.iocoder.yudao.module.member.controller.admin.newsStudyInfo;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.SortingField;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyInfo.vo.*;
import cn.iocoder.yudao.module.member.convert.newsStudyInfo.NewsStudyInfoConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyInfo.NewsStudyInfoDO;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyInfo.NewsStudyRankingDO;
import cn.iocoder.yudao.module.member.service.newsStudyInfo.NewsStudyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.error;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Api(tags = "管理后台 -运营端- 新闻信息(成果信息)")
@RestController
@RequestMapping("/member/newsStudyInfo")
@Validated
public class NewsStudyInfoController {
    @Resource
    private NewsStudyInfoService newsStudyInfoService;

    @PostMapping("/create")
    @ApiOperation("创建新闻信息(成果信息)")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:create')")
    public CommonResult<Long> createNewsStudyInfo(@Valid @RequestBody NewsStudyInfoCreateReqVO createReqVO) {
        if(StringUtils.isBlank(createReqVO.getStatus())){
            createReqVO.setStatus("0");
        }
        if("1".equals(createReqVO.getStatus())){
            createReqVO.setReleaseTime(new Date());
        }
        if(null==createReqVO.getWeight()){
            createReqVO.setWeight(0);
        }
        return success(newsStudyInfoService.createNewsStudyInfo(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新新闻信息(成果信息)")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:update')")
    public CommonResult<Boolean> updateNewsStudyInfo(@Valid @RequestBody NewsStudyInfoUpdateReqVO updateReqVO) {
        newsStudyInfoService.updateNewsStudyInfo(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除新闻信息(成果信息)")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:delete')")
    public CommonResult<Boolean> deleteNewsStudyInfo(@RequestParam("id") Long id) {
        newsStudyInfoService.deleteNewsStudyInfo(id);
        return success(true);
    }

    @PutMapping("/updateStatus")
    @ApiOperation("单个发布-取消发布")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:update')")
    public CommonResult<Boolean> updateStatus(  @RequestBody NewsStudyInfoUpdateReqVO updateReqVO) {
        if(updateReqVO.getId()==null){
            return error("ID不能为空");
        }
        if(StringUtils.isBlank(updateReqVO.getStatus())){
            return error("状态不能为空");
        }
        newsStudyInfoService.updateNewsStudyInfo(updateReqVO);
        return success(true);
    }
    @PutMapping("/updateTopping")
    @ApiOperation("是否置顶置顶")
    @TenantIgnore
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query",name = "id", value = "编号", required = true, dataTypeClass = Long.class),
            @ApiImplicitParam(paramType = "query",name = "isTop",value = "是否置顶(0:未置顶1:已置顶)", dataType ="int",required = true),
    })
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:update')")
    public CommonResult<Boolean> updateTopping(@RequestParam("id") Long id,@RequestParam("isTop") Integer isTop) {
        newsStudyInfoService.updateTopping(id,isTop);
        return success(true);
    }
    @GetMapping("/get")
    @ApiOperation("获得新闻信息(成果信息)")
    @TenantIgnore
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:query')")
    public CommonResult<NewsStudyInfoRespVO> getNewsStudyInfo(@RequestParam("id") Long id) {
        NewsStudyInfoDO newsStudyInfo = newsStudyInfoService.getNewsStudyInfo(id);
        return success(NewsStudyInfoConvert.INSTANCE.convert(newsStudyInfo));
    }

    @GetMapping("/list")
    @ApiOperation("获得新闻信息(成果信息)列表")
    @TenantIgnore
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:query')")
    public CommonResult<List<NewsStudyInfoRespVO>> getNewsStudyInfoList(@RequestParam("ids") Collection<Long> ids) {
        List<NewsStudyInfoDO> list = newsStudyInfoService.getNewsStudyInfoList(ids);
        return success(NewsStudyInfoConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得新闻信息(成果信息)分页")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:query')")
    public CommonResult<PageResult<NewsStudyInfoRespVO>> getNewsStudyInfoPage( @RequestBody NewsStudyInfoPageReqVO pageVO) {
        if(  null==pageVO.getSortField()){
            List<SortingField> list =new ArrayList<>();
            SortingField sf=new SortingField();
            sf.setField("isTop");
            sf.setOrder("desc");
            list.add(sf);
            sf=new SortingField();
            sf.setField("updateTime");
            sf.setOrder("desc");
            list.add(sf);
            pageVO.setSortingFields(list);
        }
        PageResult<NewsStudyInfoDO> pageResult = newsStudyInfoService.getNewsStudyInfoPage(pageVO);
        return success(NewsStudyInfoConvert.INSTANCE.convertPage(pageResult));
    }


    @PutMapping("/updateBatch")
    @ApiOperation("批量修改")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:update')")
    public CommonResult<Boolean> updateBatch(@RequestBody NewsStudyInfoIdReqVO newsStudyInfoIdReqVO) {
        newsStudyInfoService.updateBatch(newsStudyInfoIdReqVO);
        return success(true);
    }

    @GetMapping("/getNewsStudyRankingList")
    @ApiOperation("周榜-月榜")
    @TenantIgnore
    @ApiImplicitParam(name = "type", value = "类型:1:周榜2:月榜", required = true, example = "1", dataTypeClass = Integer.class)
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:query')")
    public CommonResult<List<NewsStudyRankingRespVO>> getNewsStudyRankingList(@RequestParam("type") Integer type) {
        List<NewsStudyRankingDO> list = newsStudyInfoService.getNewsStudyRankingList(type);
        return success(NewsStudyInfoConvert.INSTANCE.convertRangingList(list));
    }

    @DeleteMapping("/deleteNewsRankingInfo")
    @ApiOperation("不纳入榜单")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:delete')")
    public CommonResult<Boolean> deleteNewsRankingInfo(@Valid @RequestBody NewsStudyInfoIdReqVO newsStudyInfoIdReqVO ) {
        List<Long> ids=newsStudyInfoIdReqVO.getIds();
        String operate=newsStudyInfoIdReqVO.getOperate();
        Integer type=1;
        if(StringUtils.isNotBlank(operate) && CollectionUtils.isNotEmpty(ids)&&StringUtils.isNotBlank(newsStudyInfoIdReqVO.getQueryType())){
            if("month".equals(operate)){
                type=2;
            }
            for (Long id : ids) {
                newsStudyInfoService.deleteNewsRankingInfo(newsStudyInfoIdReqVO.getQueryType(),id,type);
            }
        }
        return success(true);
    }

    @PutMapping("/updateRankingInfoSort")
    @ApiOperation("榜单排序")
    @TenantIgnore
    //@PreAuthorize("@ss.hasPermission('member:news-study-info:delete')")
    public CommonResult<Boolean> updateRankingInfoSort(@Valid @RequestBody NewsStudyRankingIdReqVO rankingIdReqVO ) {
        newsStudyInfoService.rankingInfoSort(rankingIdReqVO);
        return success(true);
    }
}
